﻿using Newtonsoft.Json.Serialization;
using Sustainalytics.ESGRatings.DataContract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace Sustainalytics.ESGRatings.BusinessLogic.Tests.Integration_Tests
{
    public class ESGClientTests
    {
        private readonly ITestOutputHelper output;
        private readonly string esgComputingAddress = "http://54.76.182.88:10280/esgcomputingserv/api/v1";
        private readonly JsonMediaTypeFormatter jsonMediaTypeFormatter = new JsonMediaTypeFormatter
        {
            SerializerSettings = { ContractResolver = new CamelCasePropertyNamesContractResolver() }
        };

        public ESGClientTests(ITestOutputHelper output)
        {
            this.output = output;
        }

        [Fact]
        public async Task LoadsetTest()
        {
            Guid result;
            using (var client = new ESGClient.ESGClient(esgComputingAddress))
            {
                result = await client.LoadDataAsync(new Guid("ad5f7cdf-fcb2-4d1c-9dad-e72bc78c18d5"));
            }

            output.WriteLine(result.ToString());
        }

        [Fact]
        public async Task LoadSet_When_No_setId_Given()
        {
            Guid result;
            using (var client = new ESGClient.ESGClient(esgComputingAddress))
            {
                result = await client.LoadDataAsync(Guid.Empty);
            }

            output.WriteLine(result.ToString());
        }


        [Fact]
        public async Task SetActiveSet()
        {
            using (var client = new HttpClient())
            {
                var result = await client.PutAsync(esgComputingAddress+ "/dataprovisioning/esgsets", new ESGDataContainerDTO { setId = new Guid("79b8c56c-0dc7-4fcc-8cc6-0e73f92bef22"), isActive = true }, jsonMediaTypeFormatter).ConfigureAwait(false);


                if (result.IsSuccessStatusCode)
                {
                    await result.Content.ReadAsAsync<Guid>(new[] { jsonMediaTypeFormatter })
                        .ConfigureAwait(false);
                }
            }
        }


        [Fact]
        public async Task GetSets()
        {
            Guid activeSet;
            IEnumerable<Guid> allSets;
            using (var client = new ESGClient.ESGClient(esgComputingAddress))
            {
                activeSet = await client.GetLatestActiveSet().ConfigureAwait(false);
                 allSets = await client.GetSetIds().ConfigureAwait(false);
            }

            output.WriteLine(activeSet.ToString());
            output.WriteLine(string.Join(",", allSets));
        }

    }
}
